$(function(){
	//延时渲染模糊匹配框，否则取不到输入框长度
	setTimeout(function(){
		product = new InitAutocomplete();
		product.init({
			"_input":"#product",
			"_hideId":"#product_id",
			"itemChangeCallBack":initProc
		});
		initProduct();
		
		initProductStatus();
		
		staff = new InitAutocomplete();
		staff.init({
			"_input":"#user_name",
			"_hideId":"#user_id",
			"clearDiv":"#proc_name,#proc_id",
			"itemChangeCallBack":initProc
		});
		initStaff();
		proc = new InitAutocomplete();
		proc.init({
			"_input":"#proc_name",
			"_hideId":"#proc_id"
		});
		initProc();
	},10);
	

	$("#save").unbind().on("click",function(){
		var pro_name = $("#pro_name").val();
		var pro_id = $("#pro_name").attr("pro_id");
		var num = $("#pro_num").val();
		if(isEmpty(pro_name)&&isEmpty(pro_id)&&isEmpty(num)){
			Alert("工序数量不能为空!");
			return false;
		} else if(!check_integers(num)){
			Alert("工序数量只能为正整数!");
			return false;
		} else{
			var user_id = $("#user_name").attr("user_id");
			var data = {"PRO_ID":pro_id,"NUM":num,"USER_ID":user_id};
			$.jsonAjax("procedure","addUserProce",data,null,null,function(rtnData){
				if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
					Alert(rtnData.data.message,null,function(){
						
					});
				} else{
					Alert("员工工序录入失败！")
				}
			},null);
		}
	});
	
});

function initProduct(){
	$.jsonAjax("productServlet","getProduct",null,null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var proList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<proList.length;i++){
				dataJson.push({"ID":proList[i].PROD_ID,"VAL":proList[i].PROD_ID,"NAME":proList[i].PROD_NAME});
			}
			product.setOption(dataJson);
		} 
	},null);
}

function initProductStatus(){
	initCodeConfigComponent("#prod_status","#prod_status_id",{"codeType":"prodStatus"},null);
}

function initStaff(){
	$.jsonAjax("procedureServlet","getStaffUser",{},null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var tmpHtml = [];
			var userList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<userList.length;i++){
				dataJson.push({"ID":userList[i].USER_ID,"VAL":userList[i].USER_ID,"NAME":userList[i].USER_NAME});
			}
			staff.setOption(dataJson);
		} 
	},null);
}

function initProc(){
	var user_id = $("#user_id").val();
	var prod_id = $("#product_id").val();
	$.jsonAjax("procedureServlet","getProceList",{"USER_ID":user_id,"PROD_ID":prod_id},null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var proList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<proList.length;i++){
				dataJson.push({"ID":proList[i].PRO_ID,"VAL":proList[i].PRO_ID,"NAME":proList[i].PRO_NAME});
			}
			proc.setOption(dataJson);
		} 
	},null);
}
function initProcNum(){
	var data = {
			"PROD_ID":$.getUrlParam("prod_id"),
			"USER_ID":$.getUrlParam("user_id"),
			"PRO_ID":$.getUrlParam("proc_id")
	}
	$.jsonAjax("procedureServlet","getProceNumList",data,null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var list = rtnData.data.content.dataList;
			var dataJson = list[0];
			$("#product").val(processUndefined(dataJson.PROD_NAME));
			$("#product_id").val(processUndefined(dataJson.PROD_ID));
			$("#prod_status").val(processUndefined(dataJson.PROD_STATUS_NAME));
			$("#prod_status_id").val(processUndefined(dataJson.PROD_STATUS_CD));
			$("#user_name").val(processUndefined(dataJson.USER_NAME));
			$("#user_id").val(processUndefined(dataJson.USER_ID));
			$("#proc_name").val(processUndefined(dataJson.PRO_NAME));
			$("#proc_id").val(processUndefined(dataJson.PRO_ID));
			$("#num").val(processUndefined(dataJson.NUM));
		} 
	},null);
}

function save(){
	if($("#num").val()){
		var data = {
				"USER_ID":$("#user_id").val(),
				"PROD_ID":$("#product_id").val(),
				"PRO_ID":$("#proc_id").val(),
				"NUM":$("#num").val()
		}
		$.jsonAjax("procedureServlet","addUserProce",data,null,false,function(rtnData){
			if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
				art.dialog.message("succeed","添加工序明细成功！",function(){
					art.dialog.open.api.close();
					art.dialog.open.origin.queryListData();
				});
			} else{
				art.dialog.message("error",rtnData.data.message,null);
			}
		});
	} else{
		art.dialog.message("warning","请输入工序明细数量",null);
	}
}

function cancel(){
	art.dialog.open.api.close();
}